package util;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * 数据库工具类
 *
 * @Author:
 * @Date: 2020/2/25
 * @Description: @PACKAGE:utils
 * @version: 1.0
 */
public class DruidUtils {
    private static DruidDataSource dataSource;
    private static Connection connection;


    static {
        Properties properties = new Properties();
        InputStream inputStream = DruidUtils.class.getClassLoader().getResourceAsStream("database.properties");
        try {
            properties.load(inputStream);
            //创建连接池
            dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static DruidDataSource getDataSource() {
        return dataSource;
    }

    //增、删、改、查通用方法
    public static int executeUpdate(String sql,Object... params){
        connection = getConnection();
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            //占位符 赋值
            for(int i=0;i<params.length;i++){
                preparedStatement.setObject(i+1,params[i]);
            }
            int i = preparedStatement.executeUpdate();
            return i;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(null,preparedStatement,connection);
        }
        return 0;
    }

    //查询
    public static <T> List<T> commonsSelect(String sql, RowMapper<T> rowMapper, Object... params){
        connection = getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        //由于不知道查询到的结果是什么类型的对象，所以使用泛型规范结果
        //查询的是单个还是多个
        List<T> list = new ArrayList<T>();
        try {
            preparedStatement = connection.prepareStatement(sql);

            for (int i =0;i<params.length;i++){
                preparedStatement.setObject(i+1,params[i]);
            }
            resultSet = preparedStatement.executeQuery();
            while(resultSet.next()){
                //每循环一次拿到一行数据 ---->一个对象
                //此方法只负责查询到数据!不参与封装对象赋值
                list.add(rowMapper.getRow(resultSet));

            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(resultSet,preparedStatement,connection);
        }
        return null;
    }

    public static Connection getConnection() {
        try {
            connection = dataSource.getConnection();
            return connection;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void closeAll(ResultSet resultSet, Statement statement, Connection connection) {
        try {
            if (resultSet != null) {
                resultSet.close();
            }
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
